$(document).ready(function() {
    Highcharts.setOptions({
        global: {
            useUTC: false
        },
        colors:['#F0382B','#28A1F6','#27D834']
    });
    j = 0;
    $('#ping').click(function () {
        text = $("#Local").val();
        // alert(text);
        j = j + 1;
        //alert(j);
        if(j == 1){
            chart.series[0].name = text;
            //seriesname1 = chart.series[0].name;
            var seri1 = chart.series[0];
            if (seri1.visible) {
                seri1.hide();
            } else {
                seri1.show();
            }
        }
        else if(j == 2) {
            chart.series[1].name = text;
            // seriesname2 = chart.series[1].name;
            var seri2 = chart.series[1];
            if (seri2.visible) {
                seri2.hide();
            } else {
                seri2.show();
            }
        }
        else {
            chart.series[2].name = text;
            //seriesname3 = chart.series[2].name;
            var seri3 = chart.series[2];
            if (seri3.visible) {
                seri3.hide();
            } else {
                seri3.show();
            }
            j = 0;
        }
    });
    var chart;
    chart = new Highcharts.Chart({
        chart: {
            renderTo: 'line',  //指向的div的id属性
            // backgroundColor: '#808080',
            type: 'spline',
            marginRight: 10,
            events: {
                load: function() {

                    // set up the updating of the chart each second
                    var series1 = this.series[0];
                    var series2 = this.series[1];
                    var series3 = this.series[2];
                    var YY = {
                        yy1: 0,
                        yy2: 0,
                        yy3: 0
                    };
                    //alert("1");
                    setInterval(function() {
                        var x = (new Date()).getTime(); // current time
                        //alert(series1.name);
                        getYY1(YY,series1.name);
                        getYY2(YY,series2.name);
                        getYY3(YY,series3.name);
                        //alert("5");
                        y1 = YY.yy1;
                        y2 = YY.yy2;
                        y3 = YY.yy3;
                        // addPoint 第二个参数表示是否对图表进行重绘，当有多个参数是，需先关闭重绘，
                        // 等操作完毕后再调 chart.redraw();

                        series1.addPoint([x, y1], false, true);
                        series2.addPoint([x, y2], false, true);
                        series3.addPoint([x, y3], false, true);
                        chart.redraw();
                    }, 520);
                }
            }
        },
        title: {
            text: '<b>综合应用设计</b>'
        },
        xAxis: {
            type: 'datetime',
            tickPixelInterval: 150  //x轴上的间隔
        },
        yAxis: [{
            title: {
                text: '单位：毫秒(ms)'
            },
            plotLines: [{
                value: 0,
                width: 1,
                color: '#000000'
            }]
        }],
        tooltip: {
            formatter: function() {
                return '<b>'+ this.series.name +'</b><br/>'+
                    Highcharts.dateFormat('%Y-%m-%d %H:%M:%S', this.x) +'<br/>'+
                    Highcharts.numberFormat(this.y, 2);
            }
        },
        legend: {
            enabled: true
        },
        exporting: {
            enabled: false
        },
        credits: {
            enabled: false
        },
        series: [{
            name: 'data1',
            data:(function() {
                // generate an array of random data
                var data = [],
                    time = (new Date()).getTime(),
                    i;
                for (i = -11; i <= 0; i++) {
                    data.push({
                        x: time + i * 1000,
                        y: 0
                    });
                }
                return data;
            })(),
            visible: false
        },{
            name: 'data2',
            data:(function() {
                // generate an array of random data
                var data = [],
                    time = (new Date()).getTime(),
                    i;
                for (i = -11; i <= 0; i++) {
                    data.push({
                        x: time + i * 1000,
                        y: 0
                    });
                }
                return data;
            })(),
            visible: false
        },{
            name: 'data3',
            data:(function() {
                // generate an array of random data
                var data = [],
                    time = (new Date()).getTime(),
                    i;
                for (i = -11; i <= 0; i++) {
                    data.push({
                        x: time + i * 1000,
                        y: 0
                    });
                }
                return data;
            })(),
            visible: false
        }]
    });
});
function getYY1(theObject,name){
    var params = {};
    params.ipAddress = name;
    if(name == "data1") {
        //alert("未获取到IP");
    }
    else {
        $.ajax({
            type: "post",
            url: "http://localhost:8080/getPingInfo.action",
            data:params,
            dataType: "json",

            success: function (res, status) {
                theObject.yy1 = parseFloat(res.pingtime);
                if(res.Code == "null")
                    theObject.yy1= 0;
            },
            error: function (res, status, xhr) {
                theObject.yy1 = 0;
            }
        });
    }
}
function getYY2(theObject,name){
    var params = {};
    params.ipAddress = name;
    if(name == "data2") {
        //alert("未获取到IP");
    }
    else {
        $.ajax({
            type: "post",
            url: "http://localhost:8080/getPingInfo.action",
            data:params,
            dataType: "json",

            success: function (res, status) {
                theObject.yy2 = parseFloat(res.pingtime);
                if(res.Code == "null")
                    theObject.yy2= 0;
            },
            error: function (res, status, xhr) {
                theObject.yy2 = 0;
            }
        });
    }
}
function getYY3(theObject,name){
    var params = {};
    params.ipAddress = name;
    if(name == "data3") {
        //alert("未获取到IP");
    }
    else {
        $.ajax({
            type: "post",
            url: "http://localhost:8080/getPingInfo.action",
            data:params,
            dataType: "json",

            success: function (res, status) {
                theObject.yy3 = parseFloat(res.pingtime);
                if(res.Code == "null")
                    theObject.yy3= 0;
            },
            error: function (res, status, xhr) {
                theObject.yy3= 0;
            }
        });
    }
}